package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.qo.StatementItemQuery;
import cn.wolfcode.car.business.qo.StatementItemVO;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItem(StatementItemVO arr) {
        List<StatementItem> list = arr.getList();
        Long statementId = arr.getStatementId();
        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal totalQuantity= new BigDecimal("0.00");
        statementItemMapper.deleteBy(arr.getStatementId());
        for (StatementItem statementItem : list) {
            statementItemMapper.saveItem(statementItem,statementId);
            if (statementItem.getItemQuantity() != null){
            totalQuantity= totalQuantity.add(statementItem.getItemQuantity());}
            totalAmount = totalAmount.add(statementItem.getItemPrice().multiply( statementItem.getItemQuantity()));
        }

        statementMapper.saveItemPay(totalAmount,arr.getStatementId(),arr.getDiscountAmount(),totalQuantity);
    }

    @Override
    public void payFun(Long statementId) {
        Date date = new Date();
        User user = ShiroUtils.getUser();
        Integer statusPaid = Statement.STATUS_PAID;
        statementMapper.payFun(statementId,date,user.getId(),statusPaid);
    }


}
